package com.lianyu507.task;

import com.lianyu507.mapper.CrArticleMapper;
import com.lianyu507.mapper.DtProfessionLogMapper;
import com.lianyu507.pojo.CrArticle;
import com.lianyu507.pojo.DtProfessionLog;
import com.lianyu507.pojo.DtProfessionLogExample;
import com.lianyu507.util.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/21.
 * <p>
 * 描述:
 */

public class LogTask {

    private static Logger log = LoggerFactory.getLogger(LogTask.class);

    private static DtProfessionLogMapper professionLogMapper = SpringContextHolder.getBean(DtProfessionLogMapper.class);
    private static CrArticleMapper articleMapper = SpringContextHolder.getBean(CrArticleMapper.class);
    //private static DtOperationLogMapper dtOperationLogMapper = SpringContextHolder.getBean(DtOperationLogMapper.class);

    public static void professionLog(int logType, int logId) {
        //查询业务表中是否存在该文章记录
        String message = "";
        int count = 1;
        Map<String, Object> map = new HashMap<>();
        DtProfessionLogExample example = new DtProfessionLogExample();
        example.or().andLogIdEqualTo(logId);
        List<DtProfessionLog> professionLogs = professionLogMapper.selectByExample(example);

        CrArticle crArticle = articleMapper.selectByPrimaryKey(logId);
        if (crArticle != null) {
            message += "标题为" + crArticle.getPictureTitle() + "的文章点击量为";
            if (professionLogs != null && professionLogs.size() > 0) {
                count = professionLogs.get(0).getCount();
                map.put("count", ++count);
                message += count + "次";
                map.put("message", message);
                professionLogMapper.updateByExampleSelective(map, example);
            } else {
                message += count + "次";
                professionLogMapper.insert(CreateLog.createProfessionLog(logType, logId, count, message));
            }
        } else {
            log.error("文章不存在或被删除!");
        }
    }

   /* public static void operationLog(Map<String, Object> map) {
        byte isadmin = (byte) map.get("isadmin");
        Integer userId = (Integer) map.get("userId");
        String ip = (String) map.get("ip");
        byte referenceType = (byte) map.get("referenceType");
        Integer referenceId = (Integer) map.get("referenceId");
        String UserName = (String) map.get("userName");
        String operation = (String) map.get("operationLog");
        Date time = (Date) map.get("time");
        if (referenceType == 0) {
            String message = "用户" + UserName + "在" + time + "时登录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 1) {
            String message = "用户" + UserName + "在" + time + "时登出";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 2) {
            String message = "用户" + UserName + "在" + time + "时在cr_article表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 3) {
            String message = "用户" + UserName + "在" + time + "时在sh_user_word表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 4) {
            String message = "用户" + UserName + "在" + time + "时在cr_word_translate表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 5) {
            String message = "用户" + UserName + "在" + time + "时在mb_user表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }
        if (referenceType == 6) {
            String message = "用户" + UserName + "在" + time + "时在mb_manager表中" + operation + "id为" + referenceId + "的记录";
            dtOperationLogMapper.insert(CreateLog.createOperationLog(isadmin, userId, ip, message, referenceType, referenceId));
        }*/
    }

